package game_control;

import org.gamecontrolplus.ControlButton;
import org.gamecontrolplus.ControlDevice;
import org.gamecontrolplus.ControlIO;
import org.gamecontrolplus.ControlSlider;

import processing.core.PApplet;

public class GameControlA extends PApplet {
	ControlIO control;
	ControlDevice gameDevice;
	DummyHandler dummy = null;

	public void setup(){
		size(300,500);
		control = ControlIO.getInstance(this);

		gameDevice = control.getDevice("Controller");
//		gamepad = controll.getDevice("Logitech Extreme 3D");
//		printDevice(0, gameDevice);

		dummy = new DummyHandler();

		ControlButton b = gameDevice.getButton(4);
		if(dummy == null){
			b.plug("handlePress", ControlIO.ON_PRESS);
			b.plug("handleWhilePress", ControlIO.WHILE_PRESS);
			b.plug("handleRelease", ControlIO.ON_RELEASE);
		}
		else {
			b.plug(dummy, "handlePress", ControlIO.ON_PRESS);
			b.plug(dummy, "handleWhilePress", ControlIO.WHILE_PRESS);
			b.plug(dummy, "handleRelease", ControlIO.ON_RELEASE);
		}
	}

	public void handlePress(){
		System.out.println("# Pressed   @ " + System.currentTimeMillis());
	}

	public void handleWhilePress(){
		System.out.println("# Held down @ " + System.currentTimeMillis());
	}

	public void handleRelease(){
		System.out.println("# Released  @ " + System.currentTimeMillis());
	}

	public void draw(){
		background(255);
		for(int i = 0; i < gameDevice.getNumberOfButtons(); i ++){
			ControlButton b = gameDevice.getButton(i);
			stroke(0);
			strokeWeight(1.5f);
			fill(255);
			if(b.pressed())
				fill(255,100,100);
			ellipse(20,20 + i * 20, 8, 8);
			fill(0);
			text(b.getName() + "    " + b.getClass().getSimpleName(), 30, 24 + i *20);
		}
		int p = gameDevice.getNumberOfButtons()*20+30;

		for(int i = 0; i < gameDevice.getNumberOfSliders(); i ++){
			ControlSlider sl = gameDevice.getSlider(i);
			sl.setTolerance(0.2f);
			fill(0,128,0);
			text(""+ sl.getValue(), 20, p + i *20);
			fill(0);
			text(""+sl.getName() + "  " + sl.getTolerance(), 120, p + i *20);

		}
	}

//	public void printDevice(int id,  ControlDevice device){
//		System.out.println("========================================================================");
//		System.out.println("Device number  " + id + " is called '" + device.getName() + "' and has");
//		System.out.println("\t" + device.getNumberOfButtons() + " buttons");
//		System.out.println("\t" + device.getNumberOfSliders() + " sliders");
//		System.out.println("\t" + device.getNumberOfRumblers() + " rumblers");
//		device.printButtons();
//		device.printSliders();
//		System.out.println("------------------------------------------------------------------------\n\n");		
//	}

	public class DummyHandler {
		public void handlePress(){
			System.out.println("$ Pressed   @ " + System.currentTimeMillis());
		}

		public void handleWhilePress(){
			System.out.println("$ Held down @ " + System.currentTimeMillis());
		}

		public void handleRelease(){
			System.out.println("$ Released  @ " + System.currentTimeMillis());
		}


	}
}
